#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/msg.h>

#define MB (1024 * 1024)

char* my_alloc(int size) {
    char* ptr = (char*)malloc(size);
    if (!ptr) {
        fprintf(stderr, "malloc failed for %d bytes\n", size);
        exit(EXIT_FAILURE);
    }
    for (int i = 0; i < size; i++) {
        ptr[i] = i;
    }
    printf("Allocated %d MB, address: %p-%p\n", size / MB, ptr, ptr + size);
    return ptr;
}

void wait_for_enter() {
    int c;
    printf("Press Enter to continue...\n");
    while ((c = getchar()) != '\n' && c != EOF); // 清除缓冲区
}

int main() {
    pid_t pid = getpid();
    printf("Process PID: %d\n", pid);
    wait_for_enter();

    int ct = 72;
    while (ct--)
    {
        my_alloc(128 * MB);
    }
    wait_for_enter();
    while (1)
    {
        my_alloc(1 * MB);
    }
    
    
    return 0;
}